package ru.linnenson.darshana.infrastructure.rest;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import ru.linnenson.darshana.application.services.PhysicTasksStoreService;
import ru.linnenson.darshana.application.services.TasksCreator;
import ru.linnenson.darshana.infrastructure.persistence.models.PhysicTaskStore;
import ru.linnenson.darshana.infrastructure.rest.dto.CreatePhysicTaskDto;
import ru.linnenson.darshana.infrastructure.rest.dto.EntityIdDto;
import ru.linnenson.darshana.infrastructure.rest.dto.PhysicTaskDto;
import ru.linnenson.darshana.infrastructure.rest.dto.PhysicTasksCriteria;
import ru.linnenson.darshana.infrastructure.rest.mappers.PhysicTasksMapper;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/physic-tasks")
@RequiredArgsConstructor
public class PhysicTasksResource {

  private final TasksCreator tasksCreator;
  private final PhysicTasksMapper physicTasksMapper;
  private final PhysicTasksStoreService physicTasksStoreService;

  @GetMapping
  public ResponseEntity<List<PhysicTaskDto>> search(final PhysicTasksCriteria criteria,
                                                    @PageableDefault final Pageable pageable) {

    final List<PhysicTaskStore> tasks = physicTasksStoreService.search(criteria, pageable);

    return ResponseEntity.ok(physicTasksMapper.toDto(tasks));
  }

  @PostMapping
  public ResponseEntity<EntityIdDto> create(@RequestBody final CreatePhysicTaskDto dto) {

    final Integer id = tasksCreator.createPhysic(
        dto.getLevel(), dto.getSource(), dto.getTheme(), dto.getNumberInKim(), dto.getTags()
    );

    return ResponseEntity.ok(new EntityIdDto(id));
  }

  @PostMapping("/{taskId}/photo")
  public ResponseEntity<?> addPhoto(@PathVariable Integer taskId, @RequestParam("file") MultipartFile file)
      throws IOException {

    physicTasksStoreService.addPhoto(taskId, file.getBytes());

    return ResponseEntity.noContent().build();
  }

  @PostMapping("/{taskId}/solution-photos")
  public ResponseEntity<?> addSolutionPhoto(@PathVariable Integer taskId, @RequestParam("file") MultipartFile file)
      throws IOException {

    physicTasksStoreService.addSolutionPhoto(taskId, file.getBytes());

    return ResponseEntity.noContent().build();
  }

  @DeleteMapping("/{taskId}/solution-photos/{photoId}")
  public ResponseEntity<?> deleteSolutionPhoto(@PathVariable Integer taskId, @PathVariable String photoId) {

    physicTasksStoreService.deleteSolutionPhoto(taskId, photoId);

    return ResponseEntity.noContent().build();
  }
}
